package com.sscaipiao.android.activity.ShiShiCai;

import android.os.Bundle;
import android.view.View;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.NormalActivity;
import com.sscaipiao.android.utils.ShakeListener;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.widget.BallBase;
import com.sscaipiao.android.widget.RedBall;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Airay.Rong on 14-1-2.
 */
public class SSSiXingZhiXuanActivity extends NormalActivity implements BallBase.onClickCallback {
	private int BALL_NUM_IN_GROUP = 10;

	private List<Integer> BallIds_Qian = new ArrayList<Integer>();
	private List<Integer> BallIds_Bai = new ArrayList<Integer>();
	private List<Integer> BallIds_Shi = new ArrayList<Integer>();
	private List<Integer> BallIds_Ge = new ArrayList<Integer>();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContainerMain(R.layout.ss_si_xing_zhi_xuan_activity);
		setupUI(true, false, true, true, true);
		setTitleText("四星直选");
		setInfo("所选号码与开奖号码连续前三位、后三位或者全部按位相符，即为中奖");
		addShakeListener();
		addBalls();
	}
	private void addShakeListener() {
		ShakeListener shakeListener = new ShakeListener(this);
		shakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
			public void onShake() {
				randomChooseBalls();
			}

		});
	}
	private void addBalls() {
		BallIds_Qian.add(R.id.red_ball_qian_0);
		BallIds_Qian.add(R.id.red_ball_qian_1);
		BallIds_Qian.add(R.id.red_ball_qian_2);
		BallIds_Qian.add(R.id.red_ball_qian_3);
		BallIds_Qian.add(R.id.red_ball_qian_4);
		BallIds_Qian.add(R.id.red_ball_qian_5);
		BallIds_Qian.add(R.id.red_ball_qian_6);
		BallIds_Qian.add(R.id.red_ball_qian_7);
		BallIds_Qian.add(R.id.red_ball_qian_8);
		BallIds_Qian.add(R.id.red_ball_qian_9);

		BallIds_Bai.add(R.id.red_ball_bai_0);
		BallIds_Bai.add(R.id.red_ball_bai_1);
		BallIds_Bai.add(R.id.red_ball_bai_2);
		BallIds_Bai.add(R.id.red_ball_bai_3);
		BallIds_Bai.add(R.id.red_ball_bai_4);
		BallIds_Bai.add(R.id.red_ball_bai_5);
		BallIds_Bai.add(R.id.red_ball_bai_6);
		BallIds_Bai.add(R.id.red_ball_bai_7);
		BallIds_Bai.add(R.id.red_ball_bai_8);
		BallIds_Bai.add(R.id.red_ball_bai_9);

		BallIds_Shi.add(R.id.red_ball_shi_0);
		BallIds_Shi.add(R.id.red_ball_shi_1);
		BallIds_Shi.add(R.id.red_ball_shi_2);
		BallIds_Shi.add(R.id.red_ball_shi_3);
		BallIds_Shi.add(R.id.red_ball_shi_4);
		BallIds_Shi.add(R.id.red_ball_shi_5);
		BallIds_Shi.add(R.id.red_ball_shi_6);
		BallIds_Shi.add(R.id.red_ball_shi_7);
		BallIds_Shi.add(R.id.red_ball_shi_8);
		BallIds_Shi.add(R.id.red_ball_shi_9);

		BallIds_Ge.add(R.id.red_ball_ge_0);
		BallIds_Ge.add(R.id.red_ball_ge_1);
		BallIds_Ge.add(R.id.red_ball_ge_2);
		BallIds_Ge.add(R.id.red_ball_ge_3);
		BallIds_Ge.add(R.id.red_ball_ge_4);
		BallIds_Ge.add(R.id.red_ball_ge_5);
		BallIds_Ge.add(R.id.red_ball_ge_6);
		BallIds_Ge.add(R.id.red_ball_ge_7);
		BallIds_Ge.add(R.id.red_ball_ge_8);
		BallIds_Ge.add(R.id.red_ball_ge_9);
		for (int i = 0; i < BALL_NUM_IN_GROUP; i++) {
			getRedBallQianByIndex(i).setCallback(this);
			getRedBallBaiByIndex(i).setCallback(this);
			getRedBallShiByIndex(i).setCallback(this);
			getRedBallGeByIndex(i).setCallback(this);
		}


	}
	private BallBase getRedBallQianByIndex(int index) {
		return  (RedBall)findViewById(BallIds_Qian.get(index));
	}
	private BallBase getRedBallBaiByIndex(int index) {
		return  (RedBall)findViewById(BallIds_Bai.get(index));
	}
	private BallBase getRedBallShiByIndex(int index) {
		return  (RedBall)findViewById(BallIds_Shi.get(index));
	}
	private BallBase getRedBallGeByIndex(int index) {
		return  (RedBall)findViewById(BallIds_Ge.get(index));
	}

	@Override
	public void onBallClick(View me) {
		reCalculatorSum();
	}
	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (R.id.btn_radom == v.getId()) {
			randomChooseBalls();
		} else if (R.id.im_undo == v.getId()) {
			resetAllBalls();
			reCalculatorSum();
		} else if (R.id.btn_confirm == v.getId()) {
			//doConfirm();
		}
	}
	private void reCalculatorSum() {
		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_hint);
		int notecount = notecount();
		setMoney(String.format(sAgeFormat1, notecount + "", (notecount * 2) + ""));
	}
	private int notecount() {
		int notenum = 1;
//		for (int i = 0; i < 6; i++) {
//			notenum = notenum * (queryChoosedRedBallNum() - i);
//		}
//		notenum = notenum / 720;
//		notenum = notenum * queryChoosedBlueBallNum();
		return notenum;
	}
	private void randomChooseBalls() {
		resetAllBalls();
		Integer[] randomRedBalls = StringUtil.getRandom(10, 1);
		for (int num : randomRedBalls) {
			RedBall Ball = (RedBall) (findViewById(BallIds_Qian.get(num-1)));
			Ball.setSelected(true);
		}
		randomRedBalls = StringUtil.getRandom(10, 1);
		for (int num : randomRedBalls) {
			RedBall Ball = (RedBall) (findViewById(BallIds_Bai.get(num-1)));
			Ball.setSelected(true);
		}
		randomRedBalls = StringUtil.getRandom(10, 1);
		for (int num : randomRedBalls) {
			RedBall Ball = (RedBall) (findViewById(BallIds_Shi.get(num-1)));
			Ball.setSelected(true);
		}
		randomRedBalls = StringUtil.getRandom(10, 1);
		for (int num : randomRedBalls) {
			RedBall Ball = (RedBall) (findViewById(BallIds_Ge.get(num-1)));
			Ball.setSelected(true);
		}
		reCalculatorSum();
	}

	private void resetAllBalls() {
		for (int i = 0; i < BallIds_Qian.size(); i++) {
			RedBall ball = (RedBall) findViewById(BallIds_Qian.get(i));
			ball.reset();
		}
		for (int i = 0; i < BallIds_Bai.size(); i++) {
			RedBall ball = (RedBall) findViewById(BallIds_Bai.get(i));
			ball.reset();
		}
		for (int i = 0; i < BallIds_Shi.size(); i++) {
			RedBall ball = (RedBall) findViewById(BallIds_Shi.get(i));
			ball.reset();
		}
		for (int i = 0; i < BallIds_Ge.size(); i++) {
			RedBall ball = (RedBall) findViewById(BallIds_Ge.get(i));
			ball.reset();
		}
	}
}